home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio / Patchmix / Source / Rand.m < prev    next >
Text File  |  1992-08-01  |  4KB  |  178 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Rand.h"
  5. #import "Instrum.h"
  6. #import <appkit/graphics.h>
  7. #import "Statement.h"
  8.  
  9. @implementation Rand
  10.  
  11. + initialize
  12. {
  13.        image = [NXImage findImageNamed:"Rand"];
  14.     ampOffset.x = 30;
  15.     ampOffset.y = 52;
  16.     outOffset.x = 30;
  17.     outOffset.y = 8;
  18.     
  19.     return self;
  20. }
  21.  
  22. - init
  23. {
  24.     written = NO;
  25.     size.width = 80.;
  26.     size.height = 80.;
  27.     center.x = size.width/2.;
  28.     center.y = size.height/2.;
  29.     strcpy(type,"Rand");
  30.     strcpy(name,"rrand");
  31.     
  32.     amp = [[Param alloc] init:self :&Offset];
  33.     out = [[Param alloc] init:self :&outOffset];
  34.  
  35.     [amp setTitle:"Amp:"];
  36.     [out setTitle:"Out:"];
  37.  
  38.     paramList = [[List alloc] initCount:4];
  39.     [paramList addObject:amp];
  40.     [paramList addObject:out];
  41.     
  42.     [Inst putUgenInList:self];
  43.     
  44.     return self;
  45. }
  46.  
  47. - remove
  48. {
  49.     id cp,param;
  50.     int i;
  51.                             // make sure not connected first 
  52.     for(i = 0; i < ([paramList count]); i++) {    
  53.         param = [paramList objectAt:i];
  54.         if(cp = [param getConnectedParam]) 
  55.             return 0;
  56.     }
  57.                             // remove ugen from list
  58.     [Inst removeUgenFromList:self];
  59.     return self;
  60. }
  61.  
  62. - (NXImage *)getImage
  63. {
  64.     return image;
  65. }
  66.  
  67. - move:(NXPoint *)newloc
  68. {
  69.     location = *newloc;
  70.     [amp move:&location];
  71.     [out move:&location];
  72.     return self;
  73. }
  74.  
  75. - findParamAtPoint:(NXPoint *)point
  76. {
  77.     NXRect *rect;
  78.     int i;
  79.     id param;
  80.     
  81.     for(i = 0; i < [paramList count]; i++) {
  82.         param = [paramList objectAt:i];
  83.         rect = [param getRect];
  84.         if(NXMouseInRect(point,rect,NO))
  85.             return param;
  86.     }
  87.     //printf("no param found\n");
  88.     return nil;
  89. }
  90.  
  91. - writeUgen
  92. {
  93.     /*     for amp param:
  94.             see if there is input ugen
  95.                 if so, grab the output (ug?) and multiply it by this
  96.                     param value
  97.                 if not just use the param value
  98.          after all params done, write relevant code into lists
  99.             (declarations, assignments and loop statements)
  100.             i.e. ug? = oscil(,,,,);
  101.         go to output param connected ugen and call its "writeUgen"
  102.     */
  103.     BOOL rm;
  104.     char ampVar[10];
  105.     int i;
  106.     int parent[3];
  107.     id param;
  108.     id ug;
  109.     id nupar;
  110.     char str[50];
  111.     char par[20];
  112.     
  113.     //printf("writing ugen %d\n",index);
  114.     sprintf(str,"\tfloat ug%d;\n",index);
  115.     [Inst putVarInList:str];
  116.     sprintf(str,"\tfloat amp%d;\n",index);
  117.     [Inst putVarInList:str];
  118.     sprintf(str,"\tfloat out%d;\n",index);
  119.     [Inst putVarInList:str];
  120.  
  121.     for(i = 0; i < ([paramList count]-1); i++) {    
  122.         param = [paramList objectAt:i];
  123.         if(nupar = [param getConnectedParam]) {
  124.             ug = [nupar getUgen];
  125.             if([ug getWritten] == NO) { 
  126.                 parent[i] = [[ug writeUgen] getIndex];
  127.             }
  128.             else  {
  129.                 parent[i] = 0;
  130.             }
  131.             //printf("parent[%d] = %d\n",i,parent[i]);
  132.         }
  133.         else 
  134.             parent[i] = 0;
  135.         
  136.     }
  137.     
  138.     if(parent[0]) {     // amplitude input ugen    
  139.     //printf("input ugen to rand amp \n");
  140.         rm = YES;
  141.         sprintf(str,"\t\tamp%d = %.2f;\n",index, 
  142.             atof([[paramList objectAt:0] getValue]));
  143.         [Inst putLoopInList:str];
  144.         sprintf(str,"\tfloat amprm%d;\n",index);
  145.         [Inst putVarInList:str];
  146.         sprintf(str,"\t\tamprm%d = amp%d * ug%d;\n",index,  
  147.             index,parent[0]);
  148.         [Inst putLoopInList:str];
  149.     }
  150.     else {                // amp preset or default
  151.         rm = NO;
  152.         //printf("amp preset or default\n");
  153.         sprintf(str,"\tamp%d = %.2f;\n",index,atof([[paramList objectAt:0] getValue]));
  154.         [Inst putAssignInList:str];
  155.     }
  156.     
  157.     if(rm)
  158.         sprintf(ampVar,"amprm%d",index);
  159.     else
  160.         sprintf(ampVar,"amp%d",index);
  161.         
  162.                     // output amplitude multiplier:
  163.     strcpy(par,[[paramList objectAt:1] getValue]);
  164.     if(par[0] == 'p' && par[1] == '[')
  165.         sprintf(str,"\tout%d = %s;\n",index,par);
  166.     else
  167.         sprintf(str,"\tout%d = %.2f;\n", index, atof([[paramList objectAt:1] getValue]));
  168.     [Inst putAssignInList:str];
  169.  
  170.     sprintf(str,"\t\tug%d = %s()*%s*out%d;\n", index, name, ampVar, index);
  171.     [Inst putLoopInList:str];
  172.     
  173.     written = YES;
  174.     return self;    
  175. }
  176.  
  177. @end
  178.